﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormAirPressureTank : System.Windows.Forms.Form
	{
		public FormAirPressureTank()
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.InitParameterSetting();
		}

		private void btnCaculate_Click(object sender, EventArgs e)
		{
			if (this.GetPressureCalculatePara() && this.GetVolumeCalculatePara())
			{
				this.P1 = (this.H1 + this.H2 + this.H4) / 102.0;
				this.P2 = this.P1 + 0.098 / (this.αb - 0.098);
				this.Vx2 = this.C * this.qb / (double)(4 * this.n);
				this.V = this.β * this.Vx2 / (1.0 - this.αb);
				this.tbP1.Text = this.P1.ToString("0.000");
				this.tbP2.Text = this.P2.ToString("0.000");
				this.tbVx2.Text = this.Vx2.ToString("0.000");
				this.tbV.Text = this.V.ToString("0.000");
				this.UserSettingWrite();
			}
		}

		private void tbnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private bool GetPressureCalculatePara()
		{
			if (this.tbαb.Text == "" || this.tbh1.Text == "" || this.tbh2h3.Text == "" || this.tbh4.Text == "")
			{
				YJKMessageBox.Information("工作压力计算的各项参数均不能空");
				return false;
			}
			if (double.Parse(this.tbαb.Text) <= 0.0 || double.Parse(this.tbαb.Text) >= 1.0)
			{
				YJKMessageBox.Information("工作压力比αb必须大于0且小于1，宜采用0.65~0.85");
				return false;
			}
			this.αb = double.Parse(this.tbαb.Text);
			this.H1 = double.Parse(this.tbh1.Text);
			this.H2 = double.Parse(this.tbh2h3.Text);
			this.H4 = double.Parse(this.tbh4.Text);
			return true;
		}

		private bool GetVolumeCalculatePara()
		{
			if (this.tbβ.Text == "" || this.tbC.Text == "" || this.tbn.Text == "" || this.tbqb.Text == "")
			{
				YJKMessageBox.Information("容积计算的各项参数均不能空");
				return false;
			}
			if (double.Parse(this.tbβ.Text) < 1.0 || double.Parse(this.tbC.Text) < 1.0)
			{
				YJKMessageBox.Information("容积附加系数和安全系数必须大于等于1");
				return false;
			}
			this.β = double.Parse(this.tbβ.Text);
			this.C = double.Parse(this.tbC.Text);
			this.n = int.Parse(this.tbn.Text);
			this.qb = double.Parse(this.tbqb.Text);
			return true;
		}

		private void InitParameterSetting()
		{
			this.dic = this.userSetting.GetAirPressureTankSetting();
			this.tbαb.Text = this.userSetting.GetValueFromDic("alfab", "0.65", this.dic);
			this.tbh1.Text = this.userSetting.GetValueFromDic("h1", "50", this.dic);
			this.tbh2h3.Text = this.userSetting.GetValueFromDic("h2", "20", this.dic);
			this.tbh4.Text = this.userSetting.GetValueFromDic("h4", "32", this.dic);
			this.tbβ.Text = this.userSetting.GetValueFromDic("bita", "1.2", this.dic);
			this.tbn.Text = this.userSetting.GetValueFromDic("n", "6", this.dic);
			this.tbC.Text = this.userSetting.GetValueFromDic("c", "1.25", this.dic);
			this.tbqb.Text = this.userSetting.GetValueFromDic("qb", "8", this.dic);
		}

		private void UserSettingWrite()
		{
			try
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("alfab", this.tbαb.Text);
				dictionary.Add("h1", this.tbh1.Text);
				dictionary.Add("h2", this.tbh2h3.Text);
				dictionary.Add("h4", this.tbh4.Text);
				dictionary.Add("bita", this.tbβ.Text);
				dictionary.Add("n", this.tbn.Text);
				dictionary.Add("qb", this.tbqb.Text);
				dictionary.Add("c", this.tbC.Text);
				this.userSetting.WriteAirPressureTankSetting(dictionary);
			}
			catch (Exception)
			{
			}
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private Dictionary<string, string> dic;

		private double αb = 0.65;

		private double H1 = 50.0;

		private double H2 = 20.0;

		private double H4 = 32.0;

		private double P1;

		private double P2;

		private double β = 1.0;

		private double C = 1.0;

		private int n = 2;

		private double qb = 8.0;

		private double Vx2;

		private double V;
	}
}
